package IC.AST;

import IC.SymbolTables.*;


public abstract class ASTNode
{
	protected ASTNode _father;
	protected Interval _lines;
	protected java.util.Vector<ASTNode> _sons = new java.util.Vector<ASTNode>();
	protected SymbolTable _symbolTable = null;
	protected IC.SymbolTables.Types.Type _type;
	
	private int _value = 0;
	private String _stringValue;
	private boolean _isValueSet = false;
	
	private int _numOfRequriedRegisters = -1;
	
	private boolean _wasFound = true;
	
	public ASTNode(Interval interval) { _lines = interval; }
	
	public ASTNode GetFather() { return _father; }
	public Interval GetInterval() { return _lines; }
	public java.util.Vector<ASTNode> GetSons() { return _sons; }
	
	public void RunOnTree(Visitor visitor)
	{
		Visit(visitor);
		for (ASTNode node : _sons)
			node.RunOnTree(visitor);
	}
	
	public void RunBottomUpOnTree(Visitor visitor)
	{
		for (ASTNode node : _sons)
			node.RunBottomUpOnTree(visitor);
		Visit(visitor);
	}
	
	public boolean WasFound() { return _wasFound; }
	public void SetNotFound() { _wasFound = false; }
	
	abstract public Object Visit (Visitor visitor); 
	
	public void GiveBirth(ASTNode baby)
	{
		if (baby == null)
			return;
		_lines.Concatenate(baby._lines);
		baby._father = this;
		_sons.add(baby);
	}
	
	public void SetType(IC.SymbolTables.Types.Type type) { _type = type; }
	public IC.SymbolTables.Types.Type GetType() { return _type; }
	
	public void SetSymbolTable (SymbolTable symbolTable) { _symbolTable = symbolTable; }
	public SymbolTable GetSymbolTable() { return _symbolTable; }	
	
	public void SetNumOfRegisters(int num) { _numOfRequriedRegisters = num; }
	public int GetNumOfRegisters() { return _numOfRequriedRegisters; }
	
	public void SetExpValue (int value) 
	{ 
		_isValueSet = true;
		_value = value;
	}
	public void SetExpValue(boolean value) { SetExpValue(value ? 1 : 0); }
	public void SetExpValueToNull() { SetExpValue(0); }
	public int GetExpValue() { return _value; }
	
	public boolean IsExpValueSet() { return _isValueSet; }
	
	public void SetExpStringValue (String value) 
	{ 
		_isValueSet = true;
		_stringValue = new String(value);
	}
	public String GetExpStringValue() { return new String(_stringValue); }

	public void ExtendLines(Interval interval) { _lines.Concatenate(interval); }
}
